package com.tools.cleanmaster.ui.card.newresult;

import android.content.ContentResolver;
import android.provider.Settings;

import java.util.Random;

/**
 * CreatedXXX on 16/8/9.
 */
public class NewResPageUtils {

    private static int mBatteryLevel = 0;
    private static int mBatteryScale = 0;
    private static float mRandomValue = 85f + new Random().nextInt(15);
    private static final int TOTAL_EXPANDABLE_TIME = 4 * 60 * 60 + 37 * 60;
    public static final String GOOGLE_PLAY_SOURCE_MEDIUM_NEW_RESULTCARD = "resultcard";

    public static double getExpandableTime(ContentResolver cr) {
        float rndResult = (mRandomValue / 100f) * getBatteryLevel() * getBrightness(cr);
        return (double) TOTAL_EXPANDABLE_TIME * rndResult;
    }

    private static float getBatteryLevel() {
        float result;
        int percent = 0;
        if (mBatteryScale != 0) {
            percent = mBatteryLevel * 100 / mBatteryScale;
        }

        if (percent <= 10) {
            result = 0.15f;
        } else if (percent <= 20) {
            result = 0.25f;
        } else if (percent <= 30) {
            result = 0.35f;
        } else if (percent <= 40) {
            result = 0.45f;
        } else if (percent <= 50) {
            result = 0.55f;
        } else if (percent <= 60) {
            result = 0.65f;
        } else if (percent <= 70) {
            result = 0.75f;
        } else if (percent <= 80) {
            result = 0.85f;
        } else if (percent <= 90) {
            result = 0.95f;
        } else {
            result = 1.0f;
        }

        return result;
    }

    private static float getBrightness(ContentResolver cr) {
        float result;
        int curBrightnessValue = Settings.System.getInt(cr, Settings.System.SCREEN_BRIGHTNESS, 0);

        curBrightnessValue = curBrightnessValue * 100 / 255;
        if (curBrightnessValue <= 20) {
            result = 0.75f;
        } else if (curBrightnessValue <= 40) {
            result = 0.80f;
        } else if (curBrightnessValue <= 60) {
            result = 0.85f;
        } else if (curBrightnessValue <= 80) {
            result = 0.95f;
        } else {
            result = 1.0f;
        }
        return result;
    }

}
